<%@ page language="java" pageEncoding="us-ascii" contentType="text/html;charset=us-ascii" %>
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel

	This file is part of CDinFusion.
	
	CDinFusion is free software: you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public 
	License in version 3 (LGPL3) as published by the Free Software
	Foundation version 3 of the License.
	
	CDinFusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	Lesser General Public License for more details.
	
	You should have received a copy of the Lesser General Public
	License along with CDinFusion; see the file COPYING. If not,
	write to the Free Software Foundation, 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
	
	If you have any questions, send an email to: megx@mpi-bremen.de.
--%>
<%--Problems: frot red/black..form no action .... --%>
<%@ page import="org.springframework.webflow.execution.*"%>
<%@ page import="java.sql.Timestamp"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="org.cdinfusion.data.CDElement"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>


<%System.out.println("<<<<<<<<<<upload>>>>>>>>>>");
HttpSession session = request.getSession();
String sessionID = session.getId();
session.setAttribute("branch", "csvupload"); 
String origin = session.getAttribute("origin").toString();
String seqID = "";
// initial has 3 states {empty, true, false}
String initial ="empty";
boolean uploaded = false;

if (origin.equals("start")){
	session.setAttribute("initial","true");
}%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<style type="text/css">
</style>
<title>file upload</title>

<script type="text/javascript"> 
function extension() {
	var file = document.submitForm.file.value;
	var origin = document.submitForm.origin.value;
	if(origin == "start" | origin == "uploadfasta"){
		var pat = /(\.fasta|\.fna|\.ffn|\.faa|\.frn|\.fas|\.seqa)/gi;
		var result = file.match(pat);
		if(!result){
			alert("The extention of your file is incorrect, please use one of the following: fasta, fna, ffn, faa, seqa.");
			document.submitForm.file.focus();
			return false;
		}
	}
	if(origin == "2" | origin == "uploadcsv"){
		var pat = /(\.csv)/gi;
		var result = file.match(pat);
		if(!result){
			alert("The extention of your file is incorrect, please use the extention csv.");
			document.submitForm.file.focus();
			return false;
		}
	}
return true;
}/*end function*/
</script>
</head>

<body>
<table border="0" width="100%" bgcolor="#2B3990" cellpadding=0 cellspacing=0>
<tr bgcolor="#ffffff">
<td width=250>
<center>
<img height="81" width="250" alt="CDinFusion Logo" src="/CDinFusion/img/logo_small_dark.png">
</center>
</td>
</tr>

<tr align="left">    
<td>
<jsp:include page="documentation/navigation.jsp"></jsp:include>
</td>
</tr>

<tr bgcolor="#ffffff">
<td>
<div class="center_div">
<br>

<c:if test="${fileUploaded}">
<%uploaded=true; %>
</c:if>

<%
if(!uploaded){
	if(origin.contains("start")|origin.contains("uploadfasta")){%>
		<center><b>SELECT A FASTA FILE TO UPLOAD</b> <br><br></center>
<%}
if(origin.contains("uploadcsv")|origin.contains("2")|origin.contains("3")){%>
	<center><b>SELECT A CSV FILE TO UPLOAD</b> <br><br></center>
	<%
	initial =  session.getAttribute("initial").toString();
}%>
<center>

<form name="submitForm" method="post" enctype="multipart/form-data" onsubmit="return extension()" action="">

<%}%>
<table>
	<c:choose>
			<c:when test='${!(message == "success")}'>
					<tr align=center><td><font color="#red"> ${message}</font></td></tr>
					<tr align=center><td><font color="#black"> ${try}</font></td></tr>
			</c:when>
			<c:otherwise>
				<font color="#black">
				</font>
			</c:otherwise>
	</c:choose>
	
	<%
		Date dt = new Date();
		System.out.println( "Date = " + dt );         
		System.out.println( "ms = " + dt.getTime()); 
		if (!uploaded) {
	%>
	<tr><td>It is recommended not to upload files larger than 250 MB!</td></tr>
	<tr align=center>
		<td>File: <input type="file" name="file"></td>
	</tr>
	<tr align=center>
		<td colspan="2" class="buttonBar">
		<input type="hidden" name="_flowExecutionKey" value="<c:out value="${flowExecutionKey}"/>">
		<input type="hidden" name="sessionID" value="<%=sessionID%>">
			<%
		
		if(origin.contains("3b1")){
			initial = (String)session.getAttribute("initial");
			if (initial.contains("false")){
			String usedSequenceIDs = "";
			List<String> uSIDs = (List<String>)session.getAttribute("usedSequenceIDs");
			for(int i=0; i< uSIDs.size(); i++){
				usedSequenceIDs= usedSequenceIDs+uSIDs.get(i).toString()+",";
			}
			%>
			<input type="hidden" name="uSIDs" value="<%=usedSequenceIDs%>">
			<%
			} //end if(initial.contains("false"))
		} //end if(origin.contains("3b1")){
		%>	
		<input type="hidden" name="origin" value="<%=origin%>">
		<input type="hidden" name="initial" value="<%=initial%>">
		<%Timestamp tstamp = new Timestamp(System.currentTimeMillis());
		System.out.println("before upload button tstamp: "+tstamp+" "+tstamp.getTime());%>
		<input class="submitbutton" type="submit" name="_eventId_submit" value="Upload"></td>
	</tr>
	<%
		}
	%>
	</table>
<%
if(!uploaded){
out.print("</form>"); 
}
%>
</center>
</div>
</td>
</tr>
<tr bgcolor="#ffffff"><td align="right"height=30>
<%
if(origin.contains("start")|origin.contains("uploadfasta")){%>
	<img height="20" width="200" alt="CDinFusion Step 1" src="/CDinFusion/img/step1.png">
<%}
if(origin.contains("2")){%>
	<img height="20" width="200" alt="CDinFusion Step 2" src="/CDinFusion/img/step2.png">
<%}
if(origin.contains("uploadcsv")){%>
	<img height="20" width="200" alt="CDinFusion Step 3" src="/CDinFusion/img/step3.png">
<%}%>
</td></tr>
<jsp:include page="documentation/bottom_info.jsp"></jsp:include>
</table>
</body>
</html>